package com.lb.activity.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lb.activity.mapper.DeptMapper;
import com.lb.activity.mapper.DeptStationMapper;
import com.lb.activity.mapper.StationMapper;
import com.lb.activity.po.Dept;
import com.lb.activity.po.DeptStation;
import com.lb.activity.po.Station;
import com.lb.activity.service.StationService;
import com.lb.activity.util.ResultUtil;
import com.lb.activity.util.SnowflakeIdWorker;
import com.lb.activity.util.emumdata.DataEnum;
import com.lb.activity.vo.StationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author liubin
 */
@Service
public class StationServiceImpl implements StationService {

    @Autowired
    private DeptStationMapper deptStationMapper;

    @Autowired
    private StationMapper stationMapper;

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private SnowflakeIdWorker idWorker;


    @Override
    public ResultUtil list(StationVo stationVo) {
        PageHelper.startPage(stationVo.getPage(),stationVo.getLimit());
        List<Station> stations = stationMapper.list(stationVo.getDeptId(), DataEnum.INSERT_STATUS.getStatus());
        PageInfo<Station> pageInfo = new PageInfo(stations);
        return ResultUtil.build(0,"",Integer.parseInt(String.valueOf(pageInfo.getTotal())), pageInfo.getList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultUtil add(StationVo stationVo) {
        Long stationId = idWorker.nextId();
        // 插入岗位详情
        Station station = Station.builder()
                .id(stationId)
                .name(stationVo.getName())
                .nameDesc(stationVo.getNameDesc())
                .status(DataEnum.INSERT_STATUS.getStatus())
                .build();
        stationMapper.insertSelective(station);
        // 插入岗位和部门的中间表
        DeptStation deptStation = DeptStation.builder()
                .id(idWorker.nextId())
                .stationId(stationId)
                .deptId(stationVo.getDeptId())
                .build();
        deptStationMapper.insertSelective(deptStation);
        return ResultUtil.ok();
    }

    @Override
    public ResultUtil update(StationVo stationVo) {
        Station station = Station.builder()
                .id(stationVo.getId())
                .name(stationVo.getName())
                .nameDesc(stationVo.getNameDesc())
                .build();
        stationMapper.updateByPrimaryKeySelective(station);
        return ResultUtil.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultUtil delete(Long id) {
        Station station = Station.builder()
                .id(id)
                .status(DataEnum.DELETE_STATUS.getStatus())
                .build();
        stationMapper.updateByPrimaryKeySelective(station);
        Example example = new Example(DeptStation.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stationId",id);
        deptStationMapper.deleteByExample(example);
        //这里还需要把用户岗位删除
        // xxxxxxxx
        return ResultUtil.ok();
    }

    @Override
    public StationVo getOneById(Long id) {
        Station station = stationMapper.selectByPrimaryKey(id);
        StationVo stationVo = new StationVo();
        BeanUtils.copyProperties(station,stationVo);
        Example example = new Example(DeptStation.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stationId",id);
        List<DeptStation> deptStations = deptStationMapper.selectByExample(example);
        if(deptStations.size() > 0){
            Dept dept = deptMapper.selectByPrimaryKey(deptStations.get(0).getDeptId());
            // 避免空指针
            stationVo.setDeptId(deptStations.get(0).getDeptId());
            stationVo.setDeptName(dept.getDeptName());
        }
        return stationVo;
    }

    @Override
    public ResultUtil stationsByDeptId(Long deptId) {
        List<Station> list = stationMapper.list(deptId, DataEnum.INSERT_STATUS.getStatus());
        return ResultUtil.ok(list);
    }
}
